﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SocialPhotos.Models;
using SocialPhotos.Models.Entities;
using SocialPhotos.Models.SqlExpressRepository;

namespace SocialPhotos.Widgets.LeftMenu
{
    public class LeftMenuWidgetController : ILeftMenuWidgetController
    {
        public LeftMenuViewModel GetViewModel(int? currentId)
        {
            LeftMenuViewModel viewModel = new LeftMenuViewModel();
            viewModel.LeftMenuItems = new List<LeftMenuItem>();

            ICategoryRepository categoryRepository = new SqlExCategoryRepository();
            IQueryable<Category> categories = categoryRepository.Categories.Select(p => p);

            // add home item
            LeftMenuItem homeItem = new LeftMenuItem();
            homeItem.Name = "Home";
            if (!currentId.HasValue || currentId.Value == 0) homeItem.IsActive = true;
            homeItem.Link = string.Format("/");
            viewModel.LeftMenuItems.Add(homeItem);

            // Add CategoryItem
            foreach (var category in categories)
            {
                LeftMenuItem item = new LeftMenuItem();
                item.Id = category.Id;
                item.Name = category.Name;
                if (currentId.HasValue && currentId.Value == category.Id)
                {
                    item.IsActive = true;
                }
                item.Link = string.Format("PhotoList/{0}/{1}", item.Id, item.Name);
                viewModel.LeftMenuItems.Add(item);
            }

            return viewModel;
        }
    }
}